package PatientLookupUtil;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.*;

import persistent.Db4oServletListener;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;

import LoginUtil.User;

/**
 * Servlet implementation class PatientLookupServlet
 */
public class PatientLookupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PatientLookupServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ObjectContainer container = 
	        (ObjectContainer)request.getAttribute(Db4oServletListener.KEY_DB4O_SESSION);
		
		final String firstName = request.getParameter("firstnamebox");
		final String lastName = request.getParameter("lastnamebox");
		
		// create a precompiled regex pattern for first and last name
		// the regex adds ".*" to the beginning and end so if an user
		// types part of a name, it will match the entire name
		final Pattern firstPattern = Pattern.compile(".*" + firstName + ".*", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		final Pattern lastPattern = Pattern.compile(".*" + lastName + ".*", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		
		// create a predicate that attempts to match to the patterns
		Predicate<User> pred = new Predicate<User>() {
			public boolean match(User user) {
				Matcher firstm = firstPattern.matcher(user.getFirstName());
				Matcher lastm = lastPattern.matcher(user.getLastName());
				
				return firstm.matches() 
					&& lastm.matches() 
					&& user.getUserType() == User.UserType.Patient;
			}
		};
			
		ObjectSet<User> rs = container.query(pred);
		
		PrintWriter pw = response.getWriter();
		
		// write page header
		pw.append("<html>\n\t<head>\n" +
        "\t\t<meta http-equiv=\"Content-Type\"" + 
           "content=\"text/html; charset=windows-1256\">\n" +
        "\t\t<title>  Lookup Results - Adventure Time MedSys  </title>\n" +
        "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n" +
        "\t</head>\n\n");
		
		// write the body and header of the table
        pw.append("<body>\n" +
        		"<center>\n" +
        		"<form NAME=\"patientlookupresults\" ACTION=\"ViewPatientServlet\" METHOD=\"GET\">\n" +
        		"<table>\n" +
        		"<tr>\n<td>\n" +
        		"<table cellpadding=\"5\" border=\"1\" style=\"foreground-color: #000000;\">\n" +
        		"<tr>\n" +
				"<th>Name</th>\n" +
				"</tr>\n");
		
		// make a table of all returned users
        while(rs.hasNext()) {
        	User user = rs.next();
        	
        	pw.append("<tr>\n" +
        			"<td>" + user.getFirstName() + " " + user.getLastName() + "</td>\n" +		// insert cells
        			"</tr>\n");
                		
        }
        
        // end the table and set up the adjacent selectionbox
        pw.append("</table>\n</td>\n" +
        		"<td>Select a patient to view details for<br><br>\n" +
        		"<select name=\"userdrop\">\n" +
        		"<item value=\"null\">select an user</item>\n");
        
        // add the options for the select
        rs.reset();
        while(rs.hasNext()) {
        	User user = rs.next();
        	pw.append("<option>" + user.getFirstName() + " " + user.getLastName() + "</option>\n");
        }
        
        // end the select and create the submit button
        pw.append("</select><br><br>\n" +
        		"<button action=\"submit\" name=\"submitbutton\">Submit</button></td>\n");
        
        // close it out
        pw.append("</tr></table>\n" +
        		"</form>\n" + 
        		"</center>\n" +
        		"</body>\n" +
        		"</html>\n");
        
		pw.close();
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
